package org.jpropelleralt.ledger.list.impl;

import org.jpropelleralt.box.BoxFeatures;
import org.jpropelleralt.box.impl.BoxFeaturesDefault;
import org.jpropelleralt.ledger.list.RecordTransformer;
import org.jpropelleralt.multiverse.Multiverse;
import org.jpropelleralt.universe.Universe;

/**
 * A simple {@link RecordTransformer} that provides one field,
 * which is the actual element itself
 * @param <T>	The type of element
 */
public class RecordTransformerDirect<T> implements RecordTransformer<T> {

	private final Class<T> valueClass;
	private final String fieldName;
	private final BoxFeatures features;
	
	private RecordTransformerDirect(Class<T> valueClass, String fieldName,
			Universe universe) {
		super();
		this.valueClass = valueClass;
		this.fieldName = fieldName;
		this.features = new BoxFeaturesDefault(this, universe);
	}

	/**
	 * Create a {@link RecordTransformerDirect}
	 * @param <T>			The type of element
	 * @param valueClass	The class of value in the field
	 * @param fieldName		The name of the field
	 * @param universe		The {@link Universe} this {@link RecordTransformerDirect} will exist in
	 * @return 				A new {@link RecordTransformerDirect}
	 */
	public static <T> RecordTransformerDirect<T> create(Class<T> valueClass, String fieldName, Universe universe) {
		return new RecordTransformerDirect<T>(valueClass, fieldName, universe);
	}
	
	/**
	 * Create a {@link RecordTransformerDirect}
	 * @param <T>			The type of element
	 * @param valueClass	The class of value in the field
	 * @param fieldName		The name of the field
	 * @return 				A new {@link RecordTransformerDirect}
	 */
	public static <T> RecordTransformerDirect<T> create(Class<T> valueClass, String fieldName) {
		return new RecordTransformerDirect<T>(valueClass, fieldName, Multiverse.getCurrentUniverse());
	}

	/**
	 * Create a {@link RecordTransformerDirect} with blank field name
	 * @param <T>			The type of element
	 * @param valueClass	The class of value in the field
	 * @return 				A new {@link RecordTransformerDirect}
	 */
	public static <T> RecordTransformerDirect<T> create(Class<T> valueClass) {
		return new RecordTransformerDirect<T>(valueClass, "", Multiverse.getCurrentUniverse());
	}

	@Override
	public boolean editable(int record, T recordObject, int field) {
		return false;
	}

	@Override
	public Class<?> fieldClass(int field) {
		return valueClass;
	}

	@Override
	public int fieldCount() {
		return 1;
	}

	@Override
	public String fieldName(int field) {
		return fieldName;
	}

	@Override
	public Object get(int record, T recordObject, int field) {
		return recordObject;
	}

	@Override
	public void set(int record, T recordObject, int field, Object value) {
	}

	@Override
	public BoxFeatures features() {
		return features;
	}

}
